package com.car.action.api;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.compress.utils.Lists;

import com.car.common.model.Sms;
import com.car.common.model.Users;
import com.jfinal.core.Controller;
import com.stonehenge.backend.tools.bak.external.SMS;
import com.utils.APIJSON;
import com.utils.RongCloudUtil;
import com.utils.StringUtil;

public class SmsController extends Controller {

	
	public static String getIpAddress(HttpServletRequest request) {  
        String ip = request.getHeader("X-forwarded-for");  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("Proxy-Client-IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("WL-Proxy-Client-IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("HTTP_CLIENT_IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getRemoteAddr();  
        }  
        return ip;  
    }  
	
	
	public void send() {
		String user_mobile = getPara("user_mobile");
		if (StringUtil.empty(user_mobile)) {
			renderText(APIJSON.result(1007));
			return;
		} 
		String type = getPara("type");
		Users users = Users.dao.findFirst("select id from users where user_mobile = '" + user_mobile + "' limit 1 ");
		if ("register".equals(type) || "wrap".equals(type)|| "auth".equals(type)) {
			if (users != null) {
				renderText(APIJSON.result(1011));
			}
		}
		
		if ("repassword".equals(type) || "unwrap".equals(type)) {
			if (users == null) {
				renderText(APIJSON.result(1012));
			}
		}
		String device = getRequest().getHeader("user-agent");

		String sign = "爱此时";
		/*
		String agent= getRequest().getHeader("user-agent");
		if(agent.contains("iPhone")||agent.contains("iPod")||agent.contains("iPad")){
			sign = "50度";
		} 
		Apache-HttpClient
		*/
		// 蹩脚刷短信的，懒得理
		if (getIpAddress(getRequest()).contains("47.242.58.83")) {
			
		} if (getIpAddress(getRequest()).contains("8.210.119.72")) {
			
		}else {
			RongCloudUtil.sendTxt("200", "140901", device +"" + getIpAddress(getRequest()));	
			Sms.dao.send_sms(user_mobile, type, sign,device);
		}
		
		//
		
		renderText(APIJSON.send(0,Lists.newArrayList(),"发送成功!"));
	}
	
	
	 public void check(){
		String user_mobile = getPara("user_mobile");
		if (StringUtil.empty(user_mobile)) {
			renderText(APIJSON.result(1007));
			return;
		} 
		String type = getPara("type");
		if (StringUtil.empty(type)) {
			renderText(APIJSON.result(4001));
			return;
		} 
		String verify = getPara("verify");
		if (StringUtil.empty(verify)) {
			renderText(APIJSON.result(1013));
			return;
		}
		Sms exist = Sms.dao.findFirst("select id from sms where mobile = '" + user_mobile +"' and type ='" + type +"' and verify = '" + verify +"' order by id desc limit 1 ");
		if (exist == null) {
			renderText(APIJSON.result(1014));
			return;
		} else {
			renderText(APIJSON.result(0));
			return;
		}
	 }
	 
}
